﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Messaging;
using Lextm.SharpSnmpLib.Security;
using System.Net.Sockets;
namespace GCPUD.Autoprov.Network
{
    class Snmp
    {
        static public List<Variable> BulkWalk(string oid, IPAddress ipAddress, string community)
        {
            VersionCode version = VersionCode.V2;
            int timeout = 1000;
            int maxRepetitions = 1000;
            List<string> returnValue = new List<string>();

            WalkMode mode = WalkMode.WithinSubtree;

            ObjectIdentifier test = new ObjectIdentifier(oid);
            List<Variable> result = new List<Variable>();
            IPEndPoint receiver = new IPEndPoint(ipAddress, 161);
            try
            {
                Messenger.BulkWalk(version, receiver, new OctetString(community), test, result, timeout, maxRepetitions, mode, null, null);
            }
            catch 
            {
                return null;
            }


            return result;
        }
        static public List<Variable> Walk(string oid, IPAddress ipAddress, string community)
        {
            if (oid == null || ipAddress == null || community == null) return null;
            VersionCode version = VersionCode.V1;
            int timeout = 1000;
            List<string> returnValue = new List<string>();


            ObjectIdentifier test = new ObjectIdentifier(oid);
            List<Variable> result = new List<Variable>();
            IPEndPoint receiver = new IPEndPoint(ipAddress, 161);
            try
            {
                Messenger.Walk(version, receiver, new OctetString(community), test, result, timeout, WalkMode.WithinSubtree);

            }
            catch (SocketException ex)
            {
                
                Console.WriteLine("SnmpWalk Socket Error: " + ex.Message);
                return null;
            }
            catch (SnmpException ex)
            {
                throw new InvalidOperationException("Check the IP Address or SNMP Community" + ex.Message);
               
            }

            return result;
        }
        static public List<ISnmpData> Get(string oid, IPAddress ipAddress, string community)
        {
            VersionCode version = VersionCode.V1;
            int timeout = 1000;
            //int maxRepetitions = 1000;

            List<Variable> vList = new List<Variable>();
            Variable test = new Variable(new ObjectIdentifier(oid));
            vList.Add(test);

            IPEndPoint receiver = new IPEndPoint(ipAddress, 161);

            List<ISnmpData> data = new List<ISnmpData>();

            try
            {
                foreach (Variable variable in
                            Messenger.Get(version, receiver, new OctetString(community), vList, timeout))
                {

                    data.Add(variable.Data);

                }
            }
            catch 
            {
                return null;
            }

            return data;
        }
    }
}
